package com.choupin.choupin.admin.service.impl;

import com.choupin.choupin.admin.mapper.ManageLotteryMapper;
import com.choupin.choupin.admin.mapper.ManageTurntableMapper;
import com.choupin.choupin.admin.service.IManageLotteryService;
import com.choupin.choupin.admin.service.exception.DeleteException;
import com.choupin.choupin.entity.Lottery;
import com.choupin.choupin.service.exception.ParameterIsEmptyException;
import com.choupin.choupin.service.exception.SelectExcepion;
import com.choupin.choupin.service.exception.UpdateException;
import com.choupin.choupin.utils.images.SaveImage;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@SuppressWarnings("ALL")
@Service
public class ManageLotteryServiceImpl implements IManageLotteryService {

    private  static final  String LOCAL_UPLOAD_PATH="D:/choupin/src/main/resources/static/upload/admin/lottery";

    private  static final  String LOCAL_FRONT_PATH="D:/uniapp/choupinSaveImage/static/certificate/";


    @Autowired
    ManageLotteryMapper manageLotteryMapper;

    @Autowired
    ManageTurntableMapper manageTurntableMapper;

    @Override
    public List<Lottery> showLotterys(Integer currentPage, Integer pagePize) {
//        参数校验
        if (currentPage==null||pagePize==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
        List<Lottery> lotteries=manageLotteryMapper.showLotterys(currentPage,pagePize);
        if (lotteries==null||lotteries.size()<=0){
            throw new SelectExcepion("抽奖池信息不存在！");
        }

        System.out.println("抽奖池信息查找成功！"+lotteries);
        return lotteries;
    }

    @Override
    public List<Lottery> selectLotterys(String id, String name) {
         String idStr=null;
         Integer param=null;
        if (id!=null&&id!=""){
          idStr="true";
          param=Integer.parseInt(id);

        }
//      搜索数据
      List<Lottery> lotteries=manageLotteryMapper.selectLotterysByIdAndName(param,idStr,name);
      if (lotteries==null||lotteries.size()<0){
         throw new SelectExcepion("搜索参数为空！");
      }
        return lotteries;
    }

    @Override
    public void deleteLottery(Integer id) {
        if (id==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
//        先要删除对应的转盘
        Integer delTurntable=manageTurntableMapper.deleteTurntablesByPoolId(id);
//        if (delTurntable!=1||delTurntable==null){
//            throw new DeleteException("删除转盘异常！");
//        }

//        删除数据
        Integer result=manageLotteryMapper.deleteLottery(id);
        if (result!=1||result==null){
            throw new DeleteException("删除错误！");
        }
        System.out.println("删除抽奖池成功！"+id);

    }

    @Override
    public void add(Lottery lottery,String phone) {
        if (lottery==null||phone==null){
            throw new ParameterIsEmptyException("参数为空！");
        }

        lottery.setCreatedUser(phone);
        lottery.setModifiedUser(phone);
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowdateString=now.format(formatter);
        LocalDateTime localDateTime=LocalDateTime.parse(nowdateString,formatter);
        lottery.setCreatedTime(localDateTime);
        lottery.setModifiedTime(localDateTime);
//        将数据持久化到抽奖池表
        manageLotteryMapper.saveLottery(lottery);
        System.out.println("添加抽奖池成功！");
    }

    @Override
    public void updateImage(String name, MultipartFile file, String phone) {
        if (name==null||file==null||phone==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
//        保存图片
        String image=SaveImage.saveImage(file,LOCAL_UPLOAD_PATH,LOCAL_FRONT_PATH,"http://10.102.228.179:5173/static/certificate/");
        if (image==null){
            throw new UpdateException("保存图片失败");
        }
        LocalDateTime now=LocalDateTime.now();
        Integer result=manageLotteryMapper.updateImage(name,image,phone,now);
        if (result==null){
            throw new UpdateException("保存图片失败");
        }
        System.out.println("更新抽奖池图片成功！");

    }

    @Override
    public Lottery updateShow(Integer id) {
      if (id==null){
          throw new ParameterIsEmptyException("参数为空！");
      }
//    查询抽奖池表信息
        Lottery lottery=manageLotteryMapper.selectLotteryById(id);
       if (lottery==null){
           throw new SelectExcepion("抽奖池不存在！");
       }
        System.out.println("查询抽奖池更新信息成功！");
        return lottery;
    }

    @Override
    public String updateLottery(Integer id, String name, String instructions, String price, String phone) {
//        参数校验
        if (phone==null){
            throw new ParameterIsEmptyException("参数为空");
        }
        String priceStr=null;
        BigDecimal bprice=null;
        if (price!=null&&price!=""){
            priceStr="true";
            bprice=BigDecimal.valueOf(Long.parseLong(price));
        }
        LocalDateTime now=LocalDateTime.now();
//        更新抽奖池数据
        Integer result=manageLotteryMapper.updateLottery(id,name,instructions,bprice,priceStr,phone,now);
        if (result==null){
            throw new UpdateException("更新失败！");
        }
        System.out.println("抽奖池数据更新成功！");
        Lottery lottery=manageLotteryMapper.selectLotteryById(id);
        if (lottery==null){
            throw new SelectExcepion("抽奖池不存在！");
        }
        return lottery.getName();
    }
}
